package com.base.log.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.log.domain.bo.TSysLogArchiveRecordBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.log.domain.vo.TSysLogArchiveRecordVo;
import com.base.log.domain.TSysLogArchiveRecord;
import com.base.log.mapper.TSysLogArchiveRecordMapper;
import com.base.log.service.ITSysLogArchiveRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 日志归档记录Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysLogArchiveRecordServiceImpl implements ITSysLogArchiveRecordService {

    private final TSysLogArchiveRecordMapper baseMapper;

    /**
     * 查询日志归档记录
     */
    @Override
    public TSysLogArchiveRecordVo queryById(String autoId){
        return baseMapper.selectVoById(autoId);
    }

    /**
     * 查询日志归档记录列表
     */
    @Override
    public TableDataInfo<TSysLogArchiveRecordVo> queryPageList(TSysLogArchiveRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysLogArchiveRecord> lqw = buildQueryWrapper(bo);
        Page<TSysLogArchiveRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询日志归档记录列表
     */
    @Override
    public List<TSysLogArchiveRecordVo> queryList(TSysLogArchiveRecordBo bo) {
        LambdaQueryWrapper<TSysLogArchiveRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysLogArchiveRecord> buildQueryWrapper(TSysLogArchiveRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysLogArchiveRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getArchiveDate() != null, TSysLogArchiveRecord::getArchiveDate, bo.getArchiveDate());
        lqw.eq(bo.getLogStartDate() != null, TSysLogArchiveRecord::getLogStartDate, bo.getLogStartDate());
        lqw.eq(bo.getLogEndDate() != null, TSysLogArchiveRecord::getLogEndDate, bo.getLogEndDate());
        lqw.eq(bo.getFileSize() != null, TSysLogArchiveRecord::getFileSize, bo.getFileSize());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), TSysLogArchiveRecord::getFileName, bo.getFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), TSysLogArchiveRecord::getFilePath, bo.getFilePath());
        lqw.eq(StringUtils.isNotBlank(bo.getArchiveDec()), TSysLogArchiveRecord::getArchiveDec, bo.getArchiveDec());
        lqw.eq(StringUtils.isNotBlank(bo.getDigest()), TSysLogArchiveRecord::getDigest, bo.getDigest());
        lqw.eq(StringUtils.isNotBlank(bo.getSignValue()), TSysLogArchiveRecord::getSignValue, bo.getSignValue());
        lqw.eq(StringUtils.isNotBlank(bo.getVerStatus()), TSysLogArchiveRecord::getVerStatus, bo.getVerStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TSysLogArchiveRecord::getOperator, bo.getOperator());
        return lqw;
    }

    /**
     * 新增日志归档记录
     */
    @Override
    public Boolean insertByBo(TSysLogArchiveRecordBo bo) {
        TSysLogArchiveRecord add = BeanUtil.toBean(bo, TSysLogArchiveRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAutoId(add.getAutoId());
        }
        return flag;
    }

    /**
     * 修改日志归档记录
     */
    @Override
    public Boolean updateByBo(TSysLogArchiveRecordBo bo) {
        TSysLogArchiveRecord update = BeanUtil.toBean(bo, TSysLogArchiveRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysLogArchiveRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除日志归档记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
